/*
 * NameGenerator.java
 *
 * Created on 12 June 2007, 19:15
 * 
 * Copyright (C)
 *
 * This program is free software; you can redistribute it and/or modify it under 
 * the terms of the GNU General Public License as published by the Free Software 
 * Foundation; either version 2 of the License, or (at your option) any later 
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with 
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple 
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */

package com.grubby.generators;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Returns random surnames, based on a realistic distribution of surnames which 
 * are obtained from a weighted source file containing names.
 * <p>
 * Names sourced from http://www.wardjc.com/surnames.htm.
 * <p>
 * NOTE: This class is largely based on original code created by Rob Newsome.  
 * It is reused with his permission.
 *
 * @author A. HARMEL-LAW. E: andrew.harmel.law@gmail.com
 * @author Rob Newsome
 * @version 0.1
 */
public class NameGenerator {
    
    public static final Logger LOGGER 
            = Logger.getLogger("com.grubby.generators.NameGenerator");
    
    /** The list of surnames read in from the provided source file */
    private List surnames = new ArrayList();
    
    /** 
     * Creates a new instance of NameGenerator 
     * @param nameFileLoc The path to the file which contains your surnames, 
     * each on a newline.
     */
    public NameGenerator(String nameFileLoc) {
        
        readInNames(nameFileLoc);
        
    }
    
    /**
     * Returns a random name from the generated list. The <Random> object used
     * will be seeded based on the current system time.
     * @return One of the names, randomly chosen from the list
     */
    public String nextRandomSurname() {
        
        Random rand = new Random(System.currentTimeMillis());
        return (String) surnames.get(rand.nextInt(surnames.size()));
        
    }
    
    /**
     * Returns a random name from the generated list.
     * <P>
     * Nullsafe - if the start <Date> is then NULL will be returned.
     * @param rand The seeded <Random> to use to select the random name
     * @return One of the names, randomly chosen from the list
     */
    public String nextRandomSurname(Random rand) {
        
        // Check for null
        if (rand == null) {
            LOGGER.log(Level.WARNING, "The random object passed as an argument " +
                    "was null.  Returning NULL.");
            return null;
        }
        
        return (String) surnames.get(rand.nextInt(surnames.size()));
    }
    
    /**
     * Reads in the names from the file at the location provided and stores 
     * them in the surnames array
     * @param surnameFileLoc the location of the file with the surnames, one 
     * per line
     */
    private void readInNames(String surnameFileLoc) {
        
        try {
            
            InputStream fileStream = NameGenerator.class
                    .getResourceAsStream(surnameFileLoc);
            
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    fileStream));
            
            while (reader.ready()) {
                surnames.add(reader.readLine());
            }
            
        } catch (Exception e) {
            LOGGER.log(Level.WARNING, "There was an error reading in the surname" +
                    "file.  Throwing a Runtime Exception.");
            throw new RuntimeException("Unable to read in surnames from the file", e);
        }
        
    }
    
}
